package com.dominator.utils.sms;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.Date;
import java.util.Map;

import com.dominator.utils.exception.ApiException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.net.URLEncoder.encode;

public class ShortUrlUtil {
    private static Logger logger = LoggerFactory.getLogger(ShortUrlUtil.class);

    public static int TIMEOUT = 30 * 1000;
    public static String ENCODING = "UTF-8";



    /**
     * 通过HttpConnection 获取返回的字符串
     *
     * @param connection
     * @return
     * @throws IOException
     */
    private static String getResponseStr(HttpURLConnection connection)
            throws IOException {
        StringBuffer result = new StringBuffer();
        int responseCode = connection.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            InputStream in = connection.getInputStream();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in, ENCODING));
            String inputLine = "";
            while ((inputLine = reader.readLine()) != null) {
                result.append(inputLine);
            }
        }
        return String.valueOf(result);
    }

    public static String getSinaShort(String longUrl){
        String resUrl = null;
        try{
            URL url = new URL("http://api.t.sina.com.cn/short_url/shorten.json");
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            //POST Request Define:
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setConnectTimeout(TIMEOUT);
            connection.setRequestMethod("GET");
            String reqUrl = "source=3271760578&url_long="+longUrl;
            connection.getOutputStream().write(reqUrl.getBytes());
            connection.connect();
            String responseStr = getResponseStr(connection);
            System.out.println("response string: " + responseStr);
            JSONArray array=JSONArray.fromObject(responseStr);
            JSONObject obj = (JSONObject) array.get(0);
            resUrl=obj.getString("url_short");


        }catch(IOException e){
            e.printStackTrace();
        }
        return resUrl;
    }






    public static void main(String[] args) {

        for (int i=0; i<100;i++ ){


            System.out.println(ShortUrlUtil.getSinaShort("https://console.qingcloud.com/login"));

            System.out.println(ShortUrlUtil.getSinaShort("https://www.pmssaas.com"));

        }


    }


}



